/**
 * @Description: 封装Token
 * @author 焦政
 * @date 2019/4/6
 */
const Service = require('egg').Service;
const jwt = require('jsonwebtoken');

class TokenService extends Service {
  /**
   * @Description: 生成Token签名
   * @author 焦政
   * @date 2019/4/6
   */
  async appendToen(payload = {}) {
    // 配置Token秘钥
    const secret = '7dcac554e6af1788fd16a5d6bf149cd4';
    const token = jwt.sign(payload, secret, {});
    return token;
  }

  /**
   * @Description: 解密Token
   * @author 焦政
   * @date 2019/4/6
   */
  async decryptToken(token) {
    return new Promise((resolve, reject) => {
      jwt.verify(token, '7dcac554e6af1788fd16a5d6bf149cd4', (error, decoded) => {
        if (error) {
          reject(error);
          return;
        }
        resolve(decoded);
      });
    });
  }
}

module.exports = TokenService;
